package com.yunhe.common.aspect;

import com.alibaba.fastjson.JSON;
import com.yunhe.common.annotation.*;
import com.yunhe.common.client.WebAuthorityClient;
import com.yunhe.common.client.WebItemClient;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.authority.Organization;
import com.yunhe.common.model.authority.RealStation;
import com.yunhe.common.model.authority.User;
import com.yunhe.common.model.company.Company;
import com.yunhe.common.model.company.Employee;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.model.office.Office;
import com.yunhe.common.tempcache.CacheMap;
import com.yunhe.common.util.CollectionUtil;
import com.yunhe.common.util.RedisClient;
import com.yunhe.common.util.ReflectUtil;
import com.yunhe.common.util.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName EnumItemAspect
 * @Description TODO
 * @Author HeTao
 * @Date 2021/2/26 17:05
 * @Version 1.0
 **/
@Component
@Aspect
@Order(-4)
public class EnumItemAspect {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private WebAuthorityClient authorityClient;

    private CacheMap<String,String> itemMap = CacheMap.getDefault();

    private List<String> arrays = Arrays.asList("double","int","short","long","class java.lang.String","float","class java.lang.Long");

    private Logger logger = LoggerFactory.getLogger(EnumItemAspect.class);



    @Pointcut("execution(* com.yunhe..service..*ServiceImpl.*(..))")
    public void enumItemAspect(){
    }

    @Around(" enumItemAspect()")
    public Object enumItemCodeTranslator(ProceedingJoinPoint pjp) throws Throwable {
        Object result = pjp.proceed();
        result = this.parseDictText(result);
        return result;
    }

    private Object parseDictText(Object result) throws Exception {
        if(result != null &&!(result.getClass().equals(String.class) || result.getClass().equals(Integer.class)) ){
            if( result.getClass().equals(PageResult.class) ){
                translator(((PageResult) result).getRecords());
            }else if( result.getClass().equals(PageImpl.class) ){
                translator(((Page) result).getContent());
            }else {
                translator(result);
            }
        }
        return result;

    }

    private void translator(Object obj) throws Exception {
        //List类型
        if (obj instanceof List) {
            List<?> list=(List<?>)obj;
            for (Object object : list) {
                if (object != null && !(object.getClass().equals(String.class) || (object.getClass().equals(Integer.class)))) {
                    dealAnnotationsInsideMethod(object);
                }
            }
        }
        //其他类型：Vo,Map
        else {
            dealAnnotationsInsideMethod(obj);
        }
    }

    @SuppressWarnings("unused")
    private void dealAnnotationsInsideMethod(Object obj) throws Exception {
        Class<?> clazz = obj.getClass();
        while (!Object.class.equals(clazz)) {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                //数据字典
                if (field.isAnnotationPresent(EnumItemCode.class)) {
                    dataDictionary(field,clazz,obj);
                }else if(field.isAnnotationPresent(UserName.class)){
                    userName(field,clazz,obj);
                }else if(field.isAnnotationPresent(OrgName.class)){
                    orgName(field,clazz,obj);
                }else if (field.isAnnotationPresent(OfficeName.class)){
                    officeName(field,clazz,obj);
                }else if (field.isAnnotationPresent(UserPhone.class)){
                    userPhone(field,clazz,obj);
                }else if(field.isAnnotationPresent(NameTitle.class)){
                    nameTitle(field,clazz,obj);
                }else if(field.isAnnotationPresent(ItemAndName.class)){
                    itemAndName(field,clazz,obj);
                }

            }
            clazz = clazz.getSuperclass();
        }
    }

    private void itemAndName(Field field, Class<?> clazz, Object obj) throws Exception {
        String nameType = field.getAnnotation(ItemAndName.class).nameType();
        switch (nameType){
            case "ITEM-OFIICE":
                itemtAndOfficeName(field,clazz,obj);
                break;
        }
    }

    private void itemtAndOfficeName(Field field, Class<?> clazz, Object obj) throws Exception {
        String code = field.getAnnotation(ItemAndName.class).nameCode();
        String fieldTilte = field.getAnnotation(ItemAndName.class).nameValue();
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(fieldTilte);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + fieldTilte + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        }else {
            String fieldValueString = null;
            try {
                fieldValueString = (String) fieldValue;
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String[] split = fieldValueString.split("/");
            List<Map<String,Object>> maps = new ArrayList<>();
            //通过code
            String enumItems = itemMap.get(code);
            if(StringUtil.isEmpty(enumItems)){
                enumItems = redisClient.get("ENUM_ITEM:"+code);
                itemMap.put(code,enumItems);
            }
            String fieldValueTitle = null;
            maps = (List<Map<String, Object>>) JSON.parse(enumItems);
            if(!CollectionUtil.isEmpty(maps)){
                List<Map<String, Object>> mapList = maps.stream().filter(map -> {
                    return split[0].equals(map.get("code"));
                }).collect(Collectors.toList());
                if(CollectionUtil.isEmpty(mapList)){
                    for (Map<String, Object> map : maps) {
                        Object titleObj = map.get("title");
                        Object codeObj = map.get("code");
                        if(codeObj.equals(split[0]) || split[0].contains(codeObj.toString())){
                            fieldValueTitle = titleObj.toString();
                        }
                    }
                }else {
                    Object titleObj = mapList.get(0).get("title");
                    Object codeObj = mapList.get(0).get("code");
                    fieldValueTitle = titleObj.toString();
                }
            }

            if(split.length>1){
                String officeStr = itemMap.get("OfficeName:" + Long.parseLong(split[1]));
                if (StringUtil.isEmpty(officeStr)) {
                    officeStr = redisClient.get("OfficeName:" + Long.parseLong(split[1]));
                    itemMap.put("OfficeName:" + Long.parseLong(split[1]), officeStr);
                }
                if (!StringUtil.isEmpty(officeStr)) {
                    Office parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(officeStr)), Office.class);
                    if (parse != null) {
                        fieldValueTitle += "/"+ parse.getOfficeTitle();
                    }
                } else {
                    try {
                        ResponseEntity<ResultObject<Office>> resEntity = authorityClient.getOfficeById(Long.parseLong(split[1]));
                        if (resEntity != null) {
                            if (resEntity.getStatusCode().is2xxSuccessful()) {
                                ResultObject body = resEntity.getBody();
                                if (body != null) {
                                    Object results = body.getResults();
                                    if (results != null) {
                                        Office office = (Office) results;
                                        if (office != null) {
                                            redisClient.set("OfficeName:" + Long.parseLong(split[1]), JSON.toJSONString(office));
                                            itemMap.put("OfficeName:" + Long.parseLong(split[1]), JSON.toJSONString(office));
                                            fieldValueTitle += "/"+ office.getOfficeTitle();
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        throw new Exception(e.getMessage());
                    }
                }
            }
            field.set(obj,fieldValueTitle);
        }
    }

    //可以复用userName的缓存
    private void userPhone(Field field, Class<?> clazz, Object obj) throws Exception {
        String userId = field.getAnnotation(UserPhone.class).userId();
        if (StringUtil.isBlank(userId)) {
            throw new Exception("用户id不能为空！");
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(userId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + userId + "的属性!");
        }

        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        }else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String userStr = itemMap.get("UserName:" + fieldValueLong);
//            if(StringUtil.isEmpty(userStr)){
//                userStr = redisClient.get("UserName:" + fieldValueLong);
//                itemMap.put("UserName:" +fieldValueLong,userStr);
//            }
            if(!StringUtil.isEmpty(userStr)){
                User parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(userStr)),User.class);
                if(parse != null ){
                    field.set(obj,parse.getPhone());
                }
            }else {
                try{
                    ResponseEntity<ResultObject<User>> resEntity = authorityClient.getUserById(fieldValueLong);
                    if(resEntity != null){
                        if(resEntity.getStatusCode().is2xxSuccessful()){
                            ResultObject body = resEntity.getBody();
                            if(body != null){
                                Object results = body.getResults();
                                if(results != null){
                                    User user = (User) results;
                                    if(user != null ){
                                        //redisClient.set("UserName:"+fieldValueLong,JSON.toJSONString(user));
                                        itemMap.put("UserName:" +fieldValueLong,JSON.toJSONString(user));
                                        field.set(obj,user.getPhone());
                                    }
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void nameTitle(Field field, Class<?> clazz, Object obj) throws Exception {
        String nameType = field.getAnnotation(NameTitle.class).nameType();
        switch (nameType){
            case "ITEM":
                dataDictionary(field,clazz,obj);
                break;
            case "ORG":
                orgName(field,clazz,obj);
                break;
            case "USER":
                userName(field,clazz,obj);
                break;
            case "OFFICE":
                officeName(field,clazz,obj);
                break;
            case "USERPHONE":
                userPhone(field,clazz,obj);
                break;
            case "RealStation":
                RealStation(field,clazz,obj);
                break;
            case "Company":
                company(field,clazz,obj);
                break;
            case "Employee":
                Employee(field,clazz,obj);
                break;
            case "RealStation-Office":
                RealStationOffice(field,clazz,obj);
                break;
        }
    }

    private void RealStationOffice(Field field, Class<?> clazz, Object obj) throws Exception {
        String code = field.getAnnotation(NameTitle.class).nameCode();
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(code);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + code + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        }else if(!"00".equals(fieldValue)){
            String fieldValueString = null;
            try {
                fieldValueString = (String) fieldValue;
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            List<String> fieldValueTitle = new ArrayList<>();
            String[] split1 = fieldValueString.split(";");
            for (String str : split1) {
                String fieldValueTitleStr = null;
                String[] split = str.split("-");
                List<Map<String,Object>> maps = new ArrayList<>();
                String realStationStr = itemMap.get("RealStationName:" + split[0]);
                if (!StringUtil.isEmpty(realStationStr)) {
                    RealStation parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(realStationStr)), RealStation.class);
                    if (parse != null) {
                        fieldValueTitleStr = StringUtil.isEmpty(parse.getAbbreviationName())?parse.getEnterpriseFullName():parse.getAbbreviationName();
                    }
                } else {
                    try {
                        ResponseEntity<ResultObject<RealStation>> resEntity = authorityClient.getRealStationById(Long.valueOf(split[0]));
                        if (resEntity != null) {
                            if (resEntity.getStatusCode().is2xxSuccessful()) {
                                ResultObject body = resEntity.getBody();
                                if (body != null) {
                                    Object results = body.getResults();
                                    if (results != null) {
                                        RealStation realStation = (RealStation) results;
                                        if (realStation != null) {
                                            itemMap.put("RealStationName:" + Long.valueOf(split[0]), JSON.toJSONString(realStation));
                                            fieldValueTitleStr = StringUtil.isEmpty(realStation.getAbbreviationName())?realStation.getEnterpriseFullName():realStation.getAbbreviationName();
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        throw new Exception(e.getMessage());
                    }
                }
                if(split.length>1){
                    String officeStr = itemMap.get("OfficeName:" + Long.parseLong(split[1]));
                    if (StringUtil.isEmpty(officeStr)) {
                        officeStr = redisClient.get("OfficeName:" + Long.parseLong(split[1]));
                        itemMap.put("OfficeName:" + Long.parseLong(split[1]), officeStr);
                    }
                    if (!StringUtil.isEmpty(officeStr)) {
                        Office parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(officeStr)), Office.class);
                        if (parse != null) {
                            fieldValueTitleStr += "-"+ parse.getOfficeTitle();
                        }
                    } else {
                        try {
                            ResponseEntity<ResultObject<Office>> resEntity = authorityClient.getOfficeById(Long.parseLong(split[1]));
                            if (resEntity != null) {
                                if (resEntity.getStatusCode().is2xxSuccessful()) {
                                    ResultObject body = resEntity.getBody();
                                    if (body != null) {
                                        Object results = body.getResults();
                                        if (results != null) {
                                            Office office = (Office) results;
                                            if (office != null) {
                                                redisClient.set("OfficeName:" + Long.parseLong(split[1]), JSON.toJSONString(office));
                                                itemMap.put("OfficeName:" + Long.parseLong(split[1]), JSON.toJSONString(office));
                                                fieldValueTitleStr += "-"+ office.getOfficeTitle();
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            throw new Exception(e.getMessage());
                        }
                    }
                }
                fieldValueTitle.add(fieldValueTitleStr);
            }

            field.set(obj,String.join(";",fieldValueTitle));
        }
    }

    private void Employee(Field field, Class<?> clazz, Object obj) throws Exception {
        String employeeId = null;
        if(field.isAnnotationPresent(NameTitle.class)){
            employeeId = field.getAnnotation(NameTitle.class).nameCode();
        }
        if (StringUtil.isBlank(employeeId)) {
            throw new Exception("企业用户id不能为空！");
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(employeeId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + employeeId + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        } else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String officeStr = itemMap.get("EmployeeName:" + fieldValueLong);
            if (!StringUtil.isEmpty(officeStr)) {
                Employee parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(officeStr)), Employee.class);
                if (parse != null) {
                    field.set(obj, parse.getTitle());
                }
            } else {
                try {
                    ResponseEntity<ResultObject<Employee>> resEntity = authorityClient.getEmployeeById(fieldValueLong);
                    if (resEntity != null) {
                        if (resEntity.getStatusCode().is2xxSuccessful()) {
                            ResultObject body = resEntity.getBody();
                            if (body != null) {
                                Object results = body.getResults();
                                if (results != null) {
                                    Employee employee = (Employee) results;
                                    if (employee != null) {
                                        itemMap.put("EmployeeName:" + fieldValueLong, JSON.toJSONString(employee));
                                        field.set(obj, employee.getTitle());
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void company(Field field, Class<?> clazz, Object obj) throws Exception {
        String companyId = null;
        if(field.isAnnotationPresent(NameTitle.class)){
            companyId = field.getAnnotation(NameTitle.class).nameCode();
        }
        if (StringUtil.isBlank(companyId)) {
            throw new Exception("企业id不能为空！");
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(companyId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + companyId + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        } else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String officeStr = itemMap.get("companyName:" + fieldValueLong);
            if (!StringUtil.isEmpty(officeStr)) {
                Company parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(officeStr)), Company.class);
                if (parse != null) {
                    field.set(obj, parse.getTitle());
                }
            } else {
                try {
                    ResponseEntity<ResultObject<Company>> resEntity = authorityClient.getCompanyById(fieldValueLong);
                    if (resEntity != null) {
                        if (resEntity.getStatusCode().is2xxSuccessful()) {
                            ResultObject body = resEntity.getBody();
                            if (body != null) {
                                Object results = body.getResults();
                                if (results != null) {
                                    Company company = (Company) results;
                                    if (company != null) {
                                        itemMap.put("companyName:" + fieldValueLong, JSON.toJSONString(company));
                                        field.set(obj, company.getTitle());
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void RealStation(Field field, Class<?> clazz, Object obj) throws Exception {
        String realStationId = null;
        if(field.isAnnotationPresent(NameTitle.class)){
            realStationId = field.getAnnotation(NameTitle.class).nameCode();
        }
        if (StringUtil.isBlank(realStationId)) {
            throw new Exception("真实园区id不能为空！");
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(realStationId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + realStationId + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        } else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String officeStr = itemMap.get("RealStationName:" + fieldValueLong);
            if (!StringUtil.isEmpty(officeStr)) {
                RealStation parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(officeStr)), RealStation.class);
                if (parse != null) {
                    field.set(obj, StringUtil.isEmpty(parse.getAbbreviationName())?parse.getEnterpriseFullName():parse.getAbbreviationName());
                }
            } else {
                try {
                    ResponseEntity<ResultObject<RealStation>> resEntity = authorityClient.getRealStationById(fieldValueLong);
                    if (resEntity != null) {
                        if (resEntity.getStatusCode().is2xxSuccessful()) {
                            ResultObject body = resEntity.getBody();
                            if (body != null) {
                                Object results = body.getResults();
                                if (results != null) {
                                    RealStation realStation = (RealStation) results;
                                    if (realStation != null) {
                                        itemMap.put("RealStationName:" + fieldValueLong, JSON.toJSONString(realStation));
                                        field.set(obj, StringUtil.isEmpty(realStation.getAbbreviationName())?realStation.getEnterpriseFullName():realStation.getAbbreviationName());
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void officeName(Field field, Class<?> clazz, Object obj) throws Exception{
        String officeId = null;
        if(field.isAnnotationPresent(OfficeName.class)){
            officeId = field.getAnnotation(OfficeName.class).officeId();
        }else if(field.isAnnotationPresent(NameTitle.class)){
            officeId = field.getAnnotation(NameTitle.class).nameCode();
        }
        if (StringUtil.isBlank(officeId)) {
            throw new Exception("厂房id不能为空！");
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(officeId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + officeId + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        } else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String officeStr = itemMap.get("OfficeName:" + fieldValueLong);
            if (StringUtil.isEmpty(officeStr)) {
                officeStr = redisClient.get("OfficeName:" + fieldValueLong);
                itemMap.put("OfficeName:" + fieldValueLong, officeStr);
            }
            if (!StringUtil.isEmpty(officeStr)) {
                Office parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(officeStr)), Office.class);
                if (parse != null) {
                    field.set(obj, parse.getOfficeTitle());
                }
            } else {
                try {
                    ResponseEntity<ResultObject<Office>> resEntity = authorityClient.getOfficeById(fieldValueLong);
                    if (resEntity != null) {
                        if (resEntity.getStatusCode().is2xxSuccessful()) {
                            ResultObject body = resEntity.getBody();
                            if (body != null) {
                                Object results = body.getResults();
                                if (results != null) {
                                    Office office = (Office) results;
                                    if (office != null) {
                                        redisClient.set("OfficeName:" + fieldValueLong, JSON.toJSONString(office));
                                        itemMap.put("OfficeName:" + fieldValueLong, JSON.toJSONString(office));
                                        field.set(obj, office.getOfficeTitle());
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void orgName(Field field, Class<?> clazz, Object obj) throws Exception {
        String orgId = null;
        if(field.isAnnotationPresent(OrgName.class)){
            orgId = field.getAnnotation(OrgName.class).orgId();
        }else if(field.isAnnotationPresent(NameTitle.class)){
            orgId = field.getAnnotation(NameTitle.class).nameCode();
        }
        if (StringUtil.isBlank(orgId)) {
            return;
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(orgId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + orgId + "的属性!");
        }
        if ("null".equals(fieldValue)) {
            //field.set(obj, null);
            return;
        } else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String orgStr = itemMap.get("OrgName:" + fieldValueLong);
//            if (StringUtil.isEmpty(orgStr)) {
//                orgStr = redisClient.get("OrgName:" + fieldValueLong);
//                itemMap.put("OrgName:" + fieldValueLong, orgStr);
//            }
            if (!StringUtil.isEmpty(orgStr)) {
                Organization parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(orgStr)), Organization.class);
                if (parse != null) {
                    field.set(obj, parse.getTitle());
                }
            } else {
                try {
                    ResponseEntity<ResultObject<Organization>> resEntity = authorityClient.getOrgById(fieldValueLong);
                    if (resEntity != null) {
                        if (resEntity.getStatusCode().is2xxSuccessful()) {
                            ResultObject body = resEntity.getBody();
                            if (body != null) {
                                Object results = body.getResults();
                                if (results != null) {
                                    Organization org = (Organization) results;
                                    if (org != null) {
                                        //redisClient.set("OrgName:" + fieldValueLong, JSON.toJSONString(org));
                                        itemMap.put("OrgName:" + fieldValueLong, JSON.toJSONString(org));
                                        field.set(obj, org.getTitle());
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void userName(Field field, Class<?> clazz, Object obj) throws Exception {
        String userId = null;
        if(field.isAnnotationPresent(UserName.class)){
            userId = field.getAnnotation(UserName.class).userId();
        }else if(field.isAnnotationPresent(NameTitle.class)){
            userId = field.getAnnotation(NameTitle.class).nameCode();
        }
        if (StringUtil.isBlank(userId)) {
            throw new Exception("用户id不能为空！");
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(userId);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + userId + "的属性!");
        }

        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        }else {
            Long fieldValueLong = null;
            try {
                fieldValueLong = Long.valueOf(fieldValue);
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            String userStr = itemMap.get("UserName:" + fieldValueLong);
//            if(StringUtil.isEmpty(userStr)){
//                userStr = redisClient.get("UserName:" + fieldValueLong);
//                itemMap.put("UserName:" +fieldValueLong,userStr);
//            }
            if(!StringUtil.isEmpty(userStr)){
                User parse = JSON.toJavaObject((JSON) JSON.toJSON(JSON.parseObject(userStr)),User.class);
                if(parse != null ){
                    field.set(obj,parse.getTitle());
                }
            }else {
                try{
                    ResponseEntity<ResultObject<User>> resEntity = authorityClient.getUserById(fieldValueLong);
                    if(resEntity != null){
                        if(resEntity.getStatusCode().is2xxSuccessful()){
                            ResultObject body = resEntity.getBody();
                            if(body != null){
                                Object results = body.getResults();
                                if(results != null){
                                    User user = (User) results;
                                    if(user != null ){
                                        //redisClient.set("UserName:"+fieldValueLong,JSON.toJSONString(user));
                                        itemMap.put("UserName:" +fieldValueLong,JSON.toJSONString(user));
                                        field.set(obj,user.getTitle());
                                    }
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    throw new Exception(e.getMessage());
                }
            }
        }
    }

    private void dataDictionary(Field field,Class<?> clazz,Object obj) throws Exception {
        String code = null;
        if(field.isAnnotationPresent(EnumItemCode.class)){
            code = field.getAnnotation(EnumItemCode.class).code();
        }else if(field.isAnnotationPresent(NameTitle.class)){
            code = field.getAnnotation(NameTitle.class).nameCode();
        }
        String fieldTilte = null;
        if(field.isAnnotationPresent(EnumItemCode.class)){
            fieldTilte = field.getAnnotation(EnumItemCode.class).fieldTitle();
        }else if(field.isAnnotationPresent(NameTitle.class)){
            fieldTilte = field.getAnnotation(NameTitle.class).fieldTitle();
        }
        if (StringUtil.isBlank(fieldTilte)) {
            if (!StringUtil.endsWith(field.getName(), "Title")) {
                throw new Exception("数据字典名必须以Title结尾!");
            }
            fieldTilte = field.getName().substring(0,field.getName().length()-5);
        }
        ReflectionUtils.makeAccessible(field);
        Method method = null;
        String methodName = ReflectUtil.getMethodName(fieldTilte);
        String fieldValue = null;
        try {
            method = clazz.getMethod(methodName);
            fieldValue = method.invoke(obj, new Object[]{}) + "";
        } catch (Exception e) {
            throw new Exception("没有" + fieldTilte + "的属性!");
        }

        if ("null".equals(fieldValue)) {
            field.set(obj, null);
        }else {
            String fieldValueString = null;
            try {
                fieldValueString = (String) fieldValue;
            } catch (ClassCastException e) {
                throw new Exception("必须注解到String类型!");
            }
            List<Map<String,Object>> maps = new ArrayList<>();
            //通过code
            String enumItems = itemMap.get(code);
            if(StringUtil.isEmpty(enumItems)){
                enumItems = redisClient.get("ENUM_ITEM:"+code);
                itemMap.put(code,enumItems);
            }
            maps = (List<Map<String, Object>>) JSON.parse(enumItems);
            if(!CollectionUtil.isEmpty(maps)){
                String finalFieldValueString = fieldValueString;
                List<Map<String, Object>> mapList = maps.stream().filter(map -> {
                    return finalFieldValueString.equals(map.get("code"));
                }).collect(Collectors.toList());
                if(CollectionUtil.isEmpty(mapList)){
                    for (Map<String, Object> map : maps) {
                        Object titleObj = map.get("title");
                        Object codeObj = map.get("code");
                        if(codeObj.equals(fieldValueString) || fieldValueString.contains(codeObj.toString())){
                            fieldValueString = fieldValueString.replace(codeObj.toString(),titleObj.toString());
                        }
                    }
                }else {
                    Object titleObj = mapList.get(0).get("title");
                    Object codeObj = mapList.get(0).get("code");
                    fieldValueString = fieldValueString.replace(codeObj.toString(),titleObj.toString());
                }
                field.set(obj,fieldValueString);
            }
        }
    }
}
